<?php
/*
    Copyright (C) 2008 - 2010 Inanta Martsanto
    
    This file is part of Koma.

    Koma is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Koma is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Koma.  If not, see <http://www.gnu.org/licenses/>.
    
    Inanta Martsanto (inanta@inationsoft.com)
*/

define('KOMA_SORT_ASCENDING', 0);
define('KOMA_SORT_DESCENDING', 1);

define('KOMA_FILTER_SIMPLE', 1);
define('KOMA_FILTER_REGEX', 0);
    
final class KomaLists
{
    public static function getFileList($path, $filters = '*', $filter_type = KOMA_FILTER_SIMPLE, $sort = KOMA_SORT_ASCENDING)
    {
        $filters = (is_array($filters)) ? $filters : (explode('|', $filters));
        $files = @scandir($path);
        $ret_files = array();
                
        foreach($files as $file)
        {
            if(!is_dir($path.'/'.$file) && $file{0} != '.')
            {
                foreach($filters as $filter)
                {
                    if($filter == '*' ||
                       ($filter_type == KOMA_FILTER_SIMPLE && in_array(substr($file, strrpos($file, '.') + 1, strlen($file) - strrpos($file, '.') + 1), $filters)) ||
                       ($filter_type == KOMA_FILTER_REGEX && @preg_match('/'.$filter.'/', $file)))
                    {
                        if(!in_array($file, $ret_files))
                        {
                            $ret_files[] = $file;
                        }   
                    }
                }
            }
        }
        
        if ($sort == KOMA_SORT_DESCENDING)
        {
            rsort($ret_files);
        }
        
        return $ret_files;
    }
    
    public static function getDirectoryList($path, $filters = '*', $filter_type = KOMA_FILTER_SIMPLE, $sort = KOMA_SORT_ASCENDING)
    {
        $files = @scandir($path);
        $filters = (is_array($filters)) ? $filters : (explode('|', $filters));
        $dir = array();
        
        foreach($files as $file)
        {
            if(is_dir($path.'/'.$file) && $file{0} != '.')
            {
                foreach($filters as $filter)
                {
                    if($filter_type == KOMA_FILTER_SIMPLE)
                    {
                        $pos = strpos($file, str_replace('%', '', $filter));
                        $rpos = strrpos($file, str_replace('%', '', $filter));                       
                    }

                    if($filter == '*' ||
                       ($filter_type == KOMA_FILTER_SIMPLE &&
                            (substr($filter, 0, 1) == '%' && substr($file, -(strlen(str_replace('%', '', $filter))), strlen(str_replace('%', '', $filter))) == str_replace('%', '', $filter) && substr($filter, -1, 1) != '%') ||
                            (substr($filter, -1, 1) == '%' && $pos === 0 && substr($filter, 0, 1) != '%') ||
                            (substr($filter, 0, 1) == '%' && substr($filter, -1, 1) == '%' && $pos == $rpos && $pos != false && substr($file, -(strlen(str_replace('%', '', $filter))), strlen(str_replace('%', '', $filter))) != str_replace('%', '', $filter))) ||
                       ($filter_type == KOMA_FILTER_REGEX && @preg_match('/'.$filter.'/', $file)))
                    {
                        if(!in_array($file, $dir))
                        {
                            $dir[] = $file;
                        }   
                    }
                }
            }
        }
        
        if ($sort == KOMA_SORT_DESCENDING)
        {
            rsort($dir);
        }
        
        return $dir;
    }
    
    public static function getMonthList()
    {
        $month = array('1' => T_('January'),
                       '2' => T_('February'),
                       '3' => T_('March'),
                       '4' => T_('April'),
                       '5' => T_('May'),
                       '6' => T_('June'),
                       '7' => T_('July'),
                       '8' => T_('August'),
                       '9' => T_('September'),
                       '10' => T_('October'),
                       '11' => T_('November'),
                       '12' => T_('December'));
        
        return $month;
    }
    
    public static function getCountryList($sort = KOMA_SORT_ASCENDING)
    {
        gettext_init(KOMA_LOCALE, 'country', KOMA_LOCALE_PATH, KOMA_CHARSET);
        
        $country_list = array (
                'AD' => T_('Andorra'),
                'AE' => T_('United Arab Emirates'),
                'AF' => T_('Afghanistan'),
                'AG' => T_('Antigua and Barbuda'),
                'AI' => T_('Anguilla'),
                'AL' => T_('Albania'),
                'AM' => T_('Armenia'),
                'AN' => T_('Netherlands Antilles'),
                'AO' => T_('Angola'),
                'AQ' => T_('Antarctica'),
                'AR' => T_('Argentina'),
                'AS' => T_('American Samoa'),
                'AT' => T_('Austria'),
                'AU' => T_('Australia'),
                'AW' => T_('Aruba'),
                'AZ' => T_('Azerbaijan'),
                'BA' => T_('Bosnia and Herzegovina'),
                'BB' => T_('Barbados'),
                'BD' => T_('Bangladesh'),
                'BE' => T_('Belgium'),
                'BF' => T_('Burkina Faso'),
                'BG' => T_('Bulgaria'),
                'BH' => T_('Bahrain'),
                'BI' => T_('Burundi'),
                'BJ' => T_('Benin'),
                'BM' => T_('Bermuda'),
                'BN' => T_('Brunei Darussalam'),
                'BO' => T_('Bolivia'),
                'BR' => T_('Brazil'),
                'BS' => T_('Bahamas'),
                'BT' => T_('Bhutan'),
                'BV' => T_('Bouvet Island'),
                'BW' => T_('Botswana'),
                'BY' => T_('Belarus'),
                'BZ' => T_('Belize'),
                'CA' => T_('Canada'),
                'CC' => T_('Cocos (Keeling) Islands'),
                'CF' => T_('Central African Republic'),
                'CG' => T_('Congo'),
                'CH' => T_('Switzerland'),
                'CI' => T_("Cote D'Ivoire (Ivory Coast)"),
                'CK' => T_('Cook Islands'),
                'CL' => T_('Chile'),
                'CM' => T_('Cameroon'),
                'CN' => T_('China'),
                'CO' => T_('Colombia'),
                'CR' => T_('Costa Rica'),
                'CS' => T_('Czechoslovakia (former)'),
                'CU' => T_('Cuba'),
                'CV' => T_('Cape Verde'),
                'CX' => T_('Christmas Island'),
                'CY' => T_('Cyprus'),
                'CZ' => T_('Czech Republic'),
                'DE' => T_('Germany'),
                'DJ' => T_('Djibouti'),
                'DK' => T_('Denmark'),
                'DM' => T_('Dominica'),
                'DO' => T_('Dominican Republic'),
                'DZ' => T_('Algeria'),
                'EC' => T_('Ecuador'),
                'EE' => T_('Estonia'),
                'EG' => T_('Egypt'),
                'EH' => T_('Western Sahara'),
                'ER' => T_('Eritrea'),
                'ES' => T_('Spain'),
                'ET' => T_('Ethiopia'),
                'FI' => T_('Finland'),
                'FJ' => T_('Fiji'),
                'FK' => T_('Falkland Islands (Malvinas)'),
                'FM' => T_('Micronesia'),
                'FO' => T_('Faroe Islands'),
                'FR' => T_('France'),
                'FX' => T_('France, Metropolitan'),
                'GA' => T_('Gabon'),
                'GB' => T_('Great Britain (UK)'),
                'GD' => T_('Grenada'),
                'GE' => T_('Georgia'),
                'GF' => T_('French Guiana'),
                'GH' => T_('Ghana'),
                'GI' => T_('Gibraltar'),
                'GL' => T_('Greenland'),
                'GM' => T_('Gambia'),
                'GN' => T_('Guinea'),
                'GP' => T_('Guadeloupe'),
                'GQ' => T_('Equatorial Guinea'),
                'GR' => T_('Greece'),
                'GS' => T_('S. Georgia and S. Sandwich Isls.'),
                'GT' => T_('Guatemala'),
                'GU' => T_('Guam'),
                'GW' => T_('Guinea-Bissau'),
                'GY' => T_('Guyana'),
                'HK' => T_('Hong Kong'),
                'HM' => T_('Heard and McDonald Islands'),
                'HN' => T_('Honduras'),
                'HR' => T_('Croatia (Hrvatska)'),
                'HT' => T_('Haiti'),
                'HU' => T_('Hungary'),
                'ID' => T_('Indonesia'),
                'IE' => T_('Ireland'),
                'IL' => T_('Israel'),
                'IN' => T_('India'),
                'IO' => T_('British Indian Ocean Territory'),
                'IQ' => T_('Iraq'),
                'IR' => T_('Iran'),
                'IS' => T_('Iceland'),
                'IT' => T_('Italy'),
                'JM' => T_('Jamaica'),
                'JO' => T_('Jordan'),
                'JP' => T_('Japan'),
                'KE' => T_('Kenya'),
                'KG' => T_('Kyrgyzstan'),
                'KH' => T_('Cambodia'),
                'KI' => T_('Kiribati'),
                'KM' => T_('Comoros'),
                'KN' => T_('Saint Kitts and Nevis'),
                'KP' => T_('Korea (North)'),
                'KR' => T_('Korea (South)'),
                'KW' => T_('Kuwait'),
                'KY' => T_('Cayman Islands'),
                'KZ' => T_('Kazakhstan'),
                'LA' => T_('Laos'),
                'LB' => T_('Lebanon'),
                'LC' => T_('Saint Lucia'),
                'LI' => T_('Liechtenstein'),
                'LK' => T_('Sri Lanka'),
                'LR' => T_('Liberia'),
                'LS' => T_('Lesotho'),
                'LT' => T_('Lithuania'),
                'LU' => T_('Luxembourg'),
                'LV' => T_('Latvia'),
                'LY' => T_('Libya'),
                'MA' => T_('Morocco'),
                'MC' => T_('Monaco'),
                'MD' => T_('Moldova'),
                'MG' => T_('Madagascar'),
                'MH' => T_('Marshall Islands'),
                'MK' => T_('Macedonia'),
                'ML' => T_('Mali'),
                'MM' => T_('Myanmar'),
                'MN' => T_('Mongolia'),
                'MO' => T_('Macau'),
                'MP' => T_('Northern Mariana Islands'),
                'MQ' => T_('Martinique'),
                'MR' => T_('Mauritania'),
                'MS' => T_('Montserrat'),
                'MT' => T_('Malta'),
                'MU' => T_('Mauritius'),
                'MV' => T_('Maldives'),
                'MW' => T_('Malawi'),
                'MX' => T_('Mexico'),
                'MY' => T_('Malaysia'),
                'MZ' => T_('Mozambique'),
                'NA' => T_('Namibia'),
                'NC' => T_('New Caledonia'),
                'NE' => T_('Niger'),
                'NF' => T_('Norfolk Island'),
                'NG' => T_('Nigeria'),
                'NI' => T_('Nicaragua'),
                'NL' => T_('Netherlands'),
                'NO' => T_('Norway'),
                'NP' => T_('Nepal'),
                'NR' => T_('Nauru'),
                'NT' => T_('Neutral Zone'),
                'NU' => T_('Niue'),
                'NZ' => T_('New Zealand (Aotearoa)'),
                'OM' => T_('Oman'),
                'PA' => T_('Panama'),
                'PE' => T_('Peru'),
                'PF' => T_('French Polynesia'),
                'PG' => T_('Papua New Guinea'),
                'PH' => T_('Philippines'),
                'PK' => T_('Pakistan'),
                'PL' => T_('Poland'),
                'PM' => T_('St. Pierre and Miquelon'),
                'PN' => T_('Pitcairn'),
                'PR' => T_('Puerto Rico'),
                'PT' => T_('Portugal'),
                'PW' => T_('Palau'),
                'PY' => T_('Paraguay'),
                'QA' => T_('Qatar'),
                'RE' => T_('Reunion'),
                'RO' => T_('Romania'),
                'RU' => T_('Russian Federation'),
                'RW' => T_('Rwanda'),
                'SA' => T_('Saudi Arabia'),
                'Sb' => T_('Solomon Islands'),
                'SC' => T_('Seychelles'),
                'SD' => T_('Sudan'),
                'SE' => T_('Sweden'),
                'SG' => T_('Singapore'),
                'SH' => T_('St. Helena'),
                'SI' => T_('Slovenia'),
                'SJ' => T_('Svalbard and Jan Mayen Islands'),
                'SK' => T_('Slovak Republic'),
                'SL' => T_('Sierra Leone'),
                'SM' => T_('San Marino'),
                'SN' => T_('Senegal'),
                'SO' => T_('Somalia'),
                'SR' => T_('Suriname'),
                'ST' => T_('Sao Tome and Principe'),
                'SU' => T_('USSR (former)'),
                'SV' => T_('El Salvador'),
                'SY' => T_('Syria'),
                'SZ' => T_('Swaziland'),
                'TC' => T_('Turks and Caicos Islands'),
                'TD' => T_('Chad'),
                'TF' => T_('French Southern Territories'),
                'TG' => T_('Togo'),
                'TH' => T_('Thailand'),
                'TJ' => T_('Tajikistan'),
                'TK' => T_('Tokelau'),
                'TM' => T_('Turkmenistan'),
                'TN' => T_('Tunisia'),
                'TO' => T_('Tonga'),
                'TP' => T_('East Timor'),
                'TR' => T_('Turkey'),
                'TT' => T_('Trinidad and Tobago'),
                'TV' => T_('Tuvalu'),
                'TW' => T_('Taiwan'),
                'TZ' => T_('Tanzania'),
                'UA' => T_('Ukraine'),
                'UG' => T_('Uganda'),
                'UK' => T_('United Kingdom'),
                'UM' => T_('US Minor Outlying Islands'),
                'US' => T_('United States'),
                'UY' => T_('Uruguay'),
                'UZ' => T_('Uzbekistan'),
                'VA' => T_('Vatican City State (Holy See)'),
                'VC' => T_('Saint Vincent and the Grenadines'),
                'VE' => T_('Venezuela'),
                'VG' => T_('Virgin Islands (British)'),
                'VI' => T_('Virgin Islands (U.S.)'),
                'VN' => T_('Viet Nam'),
                'VU' => T_('Vanuatu'),
                'WF' => T_('Wallis and Futuna Islands'),
                'WS' => T_('Samoa'),
                'YE' => T_('Yemen'),
                'YT' => T_('Mayotte'),
                'YU' => T_('Yugoslavia'),
                'ZA' => T_('South Africa'),
                'ZM' => T_('Zambia'),
                'ZR' => T_('Zaire'),
                'ZW' => T_('Zimbabwe')
            );
        
        $sort == KOMA_SORT_ASCENDING ? asort($country_list) : arsort($country_list);
        
        return $country_list;
    }
    
    public static function getTimezone()
    {
        gettext_init(KOMA_LOCALE, 'timezone', KOMA_LOCALE_PATH, KOMA_CHARSET);
        
        return array('-12' => T_('(GMT-12:00) Eniwetok, Kwajalein'),
                     '-11' => T_('(GMT-11:00) Midway Island, Samoa'),
                     '-10' => T_('(GMT-10:00) Hawaii'),
                     '-9' => T_('(GMT-9:00) Alaska'),
                     '-8' => T_('(GMT-8:00) Pacific Time (US &amp; Canada)'),
                     '-7' => T_('(GMT-7:00) Mountain Time (US &amp; Canada)'),
                     '-6' => T_('(GMT-6:00) Central Time (US &amp; Canada), Mexico City'),
                     '-5' => T_('(GMT-5:00) Eastern Time (US &amp; Canada), Bogota, Lima, Quito'),
                     '-4' => T_('(GMT-4:00) Atlantic Time (Canada), Caracas, La Paz'),
                     '-3.5' => T_('(GMT-3:30) Newfoundland'),
                     '-3' => T_('(GMT-3:00) Brasilia, Buenos Aires, Georgetown'),
                     '-2' => T_('(GMT-2:00) Mid-Atlantic'),
                     '-1' => T_('(GMT-1:00) Azores, Cape Verde Islands'),
                     '0' => T_('(GMT) Greenwich Mean Time, London, Dublin, Lisbon, Casablanca, Monrovia'),
                     '1' => T_('(GMT+1:00) Amsterdam, Berlin, Rome, Copenhagen, Brussels, Madrid, Paris'),
                     '2' => T_('(GMT+2:00) Athens, Istanbul, Minsk, Helsinki, Jerusalem, South Africa'),
                     '3' => T_('(GMT+3:00) Baghdad, Kuwait, Riyadh, Moscow, St. Petersburg'),
                     '3.5' => T_('(GMT+3:30) Tehran'),
                     '4' => T_('(GMT+4:00) Abu Dhabi, Muscat, Baku, Tbilisi'),
                     '4.5' => T_('(GMT+4:30) Kabul'),
                     '5' => T_('(GMT+5:00) Ekaterinburg, Islamabad, Karachi, Tashkent'),
                     '5.5' => T_('(GMT+5:30) Bombay, Calcutta, Madras, New Delhi'),
                     '6' => T_('(GMT+6:00) Almaty, Dhaka, Colombo'),
                     '7' => T_('(GMT+7:00) Bangkok, Hanoi, Jakarta'),
                     '8' => T_('(GMT+8:00) Beijing, Perth, Singapore, Hong Kong, Urumqi, Taipei'),
                     '9' => T_('(GMT+9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk'),
                     '9.5' => T_('(GMT+9:30) Adelaide, Darwin'),
                     '10' => T_('(GMT+10:00) Brisbane, Canberra, Melbourne, Sydney, Guam,Vlasdiostok'),
                     '11' => T_('(GMT+11:00) Magadan, Solomon Islands, New Caledonia'),
                     '12' => T_('(GMT+12:00) Auckland, Wellington, Fiji, Kamchatka, Marshall Island'));
    }
    
    public static function getLocaleList()
    {        
        return array
                    (
                        'ar_SA' => 'العربية',
                        'en_US' => 'English',
                        'de_DE' => 'Deutsch',
                        'id_ID' => 'Bahasa Indonesia',
                        'jp_JP' => '日本語',
                        'jv_ID' => 'Boso Jowo',
                        'kr_KR' => '한국어',
                        'su_ID' => 'Basa Sunda',
                        'zh_CN' => '中文'
                    );
    }
    
    public static function getInstalledLocales()
    {
        $known_locales = self::getLocaleList();
        $locale_dirs = self::getDirectoryList(KOMA_LOCALE_PATH, '^[a-z]{2}_[A-Z]{2}$', KOMA_FILTER_REGEX);
        $locales = array();
        
        $locales['en_US'] = 'English';
        
        foreach($locale_dirs as $key => $locale_dir)
        {
            if(isset($known_locales[$locale_dir]))
            {
                $locales[$locale_dir] =  $known_locales[$locale_dir];
            }
            else
            {
                $locales[$locale_dir] = $locale_dir.' (Unidentified Language)';
            }
        }

        return $locales;
    }
    
    public static function getInstalledThemes()
    {
        $dir = new KomaDirectoryInfo(KOMA_ROOT_PATH.'/themes/');
        
        return ($dir->getDirectoriesArray());
    }
    
    public static function getComponentList()
    {
        return self::getDirectoryList(KOMA_ROOT_PATH.'/components/');
    }
}
?>